import request from "@/router/axios"
import Mock from "mockjs";

const list = Mock.mock({
    "tableList|50-100":[
        {
            username:"@cname",
            "sex|1":["男","女"],
            "user_registration|11111111111-99999999999":100,
            address:"@city(true)",
            create_time:"@datetime",
            "user_type|1":["会员用户","普通用户"]
        }
    ]
})
// console.log(list)
Mock.mock(/\/list/ ,config=>{
    return {
        data:{
            list:list.tableList,
            code:200,
        }
    }
})

export const UserTableListMock = ()=>request({
    url:"/list",
    method:"get"
})
Mock.mock(/\/deleteUserList/ ,config=>{
    console.log(JSON.parse(config.body))
    let id = JSON.parse(config.body);
    let _list = list.tableList;
    if (!id) {
        return {
          code: -999,
          message: '参数不正确'
        }
      } else {
        _list = _list.filter(u => u.id !== id)
        console.log(_list)
        return {
          code: 20000,
          message: '删除成功',
          _list:_list
        }
      }
})
export const deleteUserTableList = (data)=>request({
    url:"/deleteUserList",
    method:"delete",
    data
})
// 造一个接口 
/*
    pagenum 当前第几页 必填  Number
    pagesize 一页显示多少条 必填 Number
    age 根据年龄进行模糊查询数据 不必填 Number
    接口的名： /UserTableMyList 
    请求方式 ：post,
    
    数据模拟：data:{
        pagenum:1,
        pagesize:10,
        age :1 
    }
*/ 
export const UserTableMyList = (data)=> request({
    url:"/UserTableMyList",
    method:"post",
    data
})
Mock.mock(/\/UserTableMyList/,config=>{
    console.log(JSON.parse(config.body))
    let {pagenum ,pagesize , age} = JSON.parse(config.body);
    // 获取到list的数据里面tableList
    let _list = list.tableList;
    // 总条数
    let total = _list.length;
    if(age){
        _list = _list.filter((item , index)=>{
            if(item.age.toString().indexOf(age) == -1){
                return false
            }else{
                return true
            }
        })
    }
    // 根据当前第几页 和一页显示几条 返回相应数据
    // user返回的数据
    let user=  _list.filter((item , index) =>index<pagesize*pagenum && index>=pagesize*(pagenum -1));
    // 返回数据
    return{
        data:{
            user , total
        }
    }
})
